package com.chaihu.admin.service.impl.menu;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chaihu.admin.dao.MenuServiceDao;
import com.chaihu.bean.in.admin.menu.ListMenusIn;
import com.chaihu.bean.out.admin.menu.ListMenusOut;
import com.chaihu.bean.result.ResponseResult;
import com.chaihu.intf.admin.menu.ListMenusService;
import com.chaihu.intf.icommon.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
public class ListMenusServiceImpl extends BaseService<ResponseResult<List<ListMenusOut>>, ListMenusIn> implements ListMenusService {
    @Autowired
    private MenuServiceDao menuServiceDao;

    @Override
    public ResponseResult<List<ListMenusOut>> checkHandle(ResponseResult<List<ListMenusOut>> responseResult, ListMenusIn in) {
        return responseResult;
    }

    @Override
    public ResponseResult<List<ListMenusOut>> doHandle(ResponseResult<List<ListMenusOut>> responseResult, ListMenusIn in) {
        queryMenus(responseResult, in);
        return responseResult;
    }

    private void queryMenus(ResponseResult<List<ListMenusOut>> responseResult, ListMenusIn in) {
        QueryWrapper<ListMenusIn> queryWrapper = new QueryWrapper<ListMenusIn>()
                .apply("t.status_cd= '1' and p.status_cd= '1' and t.p_id = p.p_id")
                .eq(!StringUtils.isEmpty(in.getgId()), "t.g_id", in.getgId())
                .apply(!StringUtils.isEmpty(in.getName()), "t.name like concat('%',{0},'%')", in.getName())
                .apply(!StringUtils.isEmpty(in.getmName()), "t.name like concat('%',{0},'%')", in.getmName())
                .eq(!StringUtils.isEmpty(in.getpId()), "t.p_id", in.getpId())
                .eq(!StringUtils.isEmpty(in.getDomain()), "p.domain", in.getDomain())
                .apply(!StringUtils.isEmpty(in.getpName()), "p.name like concat('%',{0},'%')", in.getpName())
                .eq(!StringUtils.isEmpty(in.getmId()), "t.m_id", in.getmId())
                .eq(!StringUtils.isEmpty(in.getIsShow()), "t.is_show", in.getIsShow())
                .apply(!StringUtils.isEmpty(in.getUrl()), "t.url like concat('%',{0},'%')", in.getUrl())
                .eq(in.getSeq() != null, "t.seq", in.getSeq())
                .eq(!StringUtils.isEmpty(in.getDescription()), "t.description", in.getDescription())
                .exists(!StringUtils.isEmpty(in.getuName()), "SELECT 1 FROM a_privilege_user ppu,u_user u WHERE ppu.p_id = p.p_id AND ppu.privilege_flag = '1' and ppu.user_id = u.user_id AND u.name = " + in.getuName() + " AND ppu.status_cd = '1' UNION SELECT 1 FROM a_privilege_user ppu, a_privilege_group ppg,a_privilege_rel ppr,u_user u WHERE ppu.p_id = ppr.pg_id AND ppr.pg_id = ppg.pg_id AND ppr.p_id = p.p_id AND ppu.privilege_flag = '1' and ppu.user_id = u.user_id AND u.name = " + in.getuName() + " AND ppu.status_cd = '1' AND ppg.status_cd = '1' AND ppr.status_cd = '1'")
                .orderByDesc("t.create_time");

        if (in.getPageDto() != null) {
            IPage<ListMenusOut> page = new Page<>(in.getPageDto().getPage(), in.getPageDto().getRow());
            IPage<ListMenusOut> listMenusOutIPage = menuServiceDao.selectListMenusByPage(page, queryWrapper);
            responseResult.setData(listMenusOutIPage.getRecords(), listMenusOutIPage);
        } else {
            List<ListMenusOut> listMenusOutIPage = menuServiceDao.selectListMenus(queryWrapper);
            responseResult.setData(listMenusOutIPage);
        }
    }

    @Override
    public ResponseResult<List<ListMenusOut>> respHandle(ResponseResult<List<ListMenusOut>> responseResult, ListMenusIn in) {
        return responseResult;
    }

    @Override
    public ResponseResult<List<ListMenusOut>> listMenus(ListMenusIn in) {
        ResponseResult<List<ListMenusOut>> result = new ResponseResult<>();
        return this.mainHandle(result, in);
    }
}
